﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Web.Script.Serialization;
using Assignment_3.Classes;
using Assignment_3.Feature___Housekeeping;

namespace Assignment_3.Feature___Reports
{
    public partial class Housekeeping : System.Web.UI.Page
    {
        #region Control Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            if (((string)Session["usertype"]) == "Reception")
            {
                Response.Redirect("~/Other - Error Pages/NoPermission.aspx");
            }

            if (!IsPostBack)
            {
                startdateTbx.Attributes.Add("ReadOnly", "true");
                enddateTbx.Attributes.Add("ReadOnly", "true");
                string currentdate = DateTime.Now.ToString("yyyy-MM-dd");
                startdateTbx.Text = currentdate;
                enddateTbx.Text = currentdate;
                enddateTbx_CalendarExtender.EndDate = Convert.ToDateTime(currentdate).AddDays(6);
                chartPanel.Visible = false;
            }
        }

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
            emptyLbl.Text = "";
            DateTime startdate = Convert.ToDateTime(startdateTbx.Text);
            DateTime enddate = Convert.ToDateTime(enddateTbx.Text);
            List<HKReport> generalList = DatabaseManager.getHousekeepingReport(startdate, enddate);//"04/04/2013", "05/05/2013");
            if (generalList.Count != 0)
            {
                chartPanel.Visible = true;

                //Table Chart
                StringBuilder data = new StringBuilder();
                JavaScriptSerializer json = new JavaScriptSerializer();
                json.Serialize(generalList, data);
                hdData.Value = data.ToString();

                string period = periodRbl.SelectedItem.Text;
                ArrayList HousekeepingChartList = new ArrayList();
                //Establish List to be store data into Column Chart
                List<HKChart> chartList = new List<HKChart>();
                if (period.Equals("Daily"))
                {
                    HousekeepingChartList = DatabaseManager.getHousekeepingByDay(startdate, enddate);

                    //Column Chart - % of Housekeeping Records based on date range
                    foreach (Object[] i in HousekeepingChartList)
                    {
                        HKChart row = new HKChart();
                        row.Period = (string)i[0];
                        row.Count = (int)i[1];
                        chartList.Add(row);
                    }
                }
                else if (period.Equals("Weekly"))
                {
                    HousekeepingChartList = DatabaseManager.getHousekeepingByWeek(startdate, enddate);

                    //Column Chart - % of Housekeeping Records based on week number
                    foreach (int[] i in HousekeepingChartList)
                    {
                        HKChart row = new HKChart();

                        //Convert Week Number to date range
                        int someYear = i[2];
                        string date = "";
                        DateTime dt = new DateTime(someYear, 1, 1).AddDays((i[0] - 1) * 7 - (i[0] == 1 ? 0 : 1));
                        date = dt.AddDays(-(int)dt.DayOfWeek).ToString("dd/MM/yyyy");
                        date += " to " + dt.AddDays(5).ToString("dd/MM/yyyy");
                        row.Period = date;
                        row.Count = i[1];
                        chartList.Add(row);
                    }

                }
                else if (period.Equals("Monthly"))
                {
                    HousekeepingChartList = DatabaseManager.getHousekeepingByMonth(startdate, enddate);
                    string[] months = { null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
                    
                    //Column Chart - % of Housekeeping Records based on month
                    foreach (int[] i in HousekeepingChartList)
                    {
                        HKChart row = new HKChart();
                        //Get the name of the month
                        for (int x = 1; x <= months.Length; x++)
                        {
                            if (i[0] == x)
                            {
                                row.Period = months[x] + " " + i[2];
                            }
                            
        }
                        row.Count = i[1];
                        chartList.Add(row);
                    }
                }

                //Draw Column Chart
                StringBuilder data2 = new StringBuilder();
                JavaScriptSerializer json2 = new JavaScriptSerializer();
                json2.Serialize(chartList, data2);
                hdData2.Value = data2.ToString();
            }

            else
            {
                emptyLbl.Text = "There is no records for this range of date.";
                chartPanel.Visible = false;
    }
}

        protected void periodRbl_SelectedIndexChanged(object sender, EventArgs e)
        {
            emptyLbl.Text = "";
            startdateTbx.Text = "";
            enddateTbx.Text = "";
            startdateVld.IsValid = false;
            enddateTbx.Enabled = false;

        }

        protected void startdateTbx_TextChanged(object sender, EventArgs e)
        {
            enddateTbx.Text = "";
            if (startdateTbx.Text != "")
            {
                enddateTbx.Enabled = true;
                enddateTbx_CalendarExtender.StartDate = Convert.ToDateTime(startdateTbx.Text);
                string period = periodRbl.SelectedItem.Text;

                //Limit enddate of the Calendar
                if (period.Equals("Daily"))
                {
                    //1 Week Limitation
                    enddateTbx_CalendarExtender.EndDate = Convert.ToDateTime(startdateTbx.Text).AddDays(6);
                }
                else if (period.Equals("Weekly"))
                {
                    //2 Months Limitation
                    enddateTbx_CalendarExtender.EndDate = Convert.ToDateTime(startdateTbx.Text).AddMonths(2);
                }
                else if (period.Equals("Monthly"))
                {
                    //1 Year Limitation
                    enddateTbx_CalendarExtender.EndDate = Convert.ToDateTime(startdateTbx.Text).AddYears(1);
                }
            }
            else
            {
                startdateVld.IsValid = false;
                enddateTbx.Enabled = false;
            }
        }
        #endregion
    }
}
#region other classes
public class HKChart
{
    private string period;
    private int count;

    public string Period
    {
        get { return period; }
        set { period = value; }
    }

    public int Count
    {
        get { return count; }
        set { count = value; }
    }

}
#endregion